//
//  MEUtil.m
//  MapEditor
//
//  Created by Eric on 3/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MEUtil.h"

@implementation MEUtil

+(BOOL) isInPolygon:(NSArray*) points point:(CGPoint) p
{
    NSInteger n = [points count];
    if (n<3) {
        return NO;
    }else{
        int counter = 0;
        int i;
        double xinters;
        CGPoint p1,p2;
        
        p1 = NSPointToCGPoint([[points objectAtIndex:0] pointValue]);
        for (i=1;i<=n;i++) {
            p2 = NSPointToCGPoint([[points objectAtIndex:i % n] pointValue]);
            if (p.y > MIN(p1.y,p2.y)) {
                if (p.y <= MAX(p1.y,p2.y)) {
                    if (p.x <= MAX(p1.x,p2.x)) {
                        if (p1.y != p2.y) {
                            xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
                            if (p1.x == p2.x || p.x <= xinters)
                                counter++;
                        }
                    }
                }
            }
            p1 = p2;
        }
        
        if (counter % 2 == 0)
            return NO;
        else
            return YES;
    }
}


+(BOOL) isClockWise:(NSArray*) ps
{
    NSInteger n = [ps count];
    NSInteger i,j,k;
    NSInteger count = 0;
    double z;
    
    if (n < 3)
        return(0);
    
    for (i=0;i<n;i++) {
        j = (i + 1) % n;
        k = (i + 2) % n;
        
        CGPoint pi = NSPointToCGPoint([[ps objectAtIndex:i] pointValue]);
        CGPoint pj = NSPointToCGPoint([[ps objectAtIndex:j] pointValue]);
        CGPoint pk = NSPointToCGPoint([[ps objectAtIndex:k] pointValue]);
        
        z  = (pj.x - pi.x) * (pk.y - pj.y);
        z -= (pj.y - pi.y) * (pk.x - pj.x);
        if (z < 0)
            count--;
        else if (z > 0)
            count++;
    }
    if (count > 0)
        return YES;
    else
        return NO;
}

+(CGFloat)distance:(CGPoint)p1 p2:(CGPoint)p2 p3:(CGPoint)p3{
    CGFloat p1x = p1.x;
    CGFloat p1y = p1.y;
    
    CGFloat p2x = p2.x;
    CGFloat p2y = p2.y;
    
    CGFloat p3x = p3.x;
    CGFloat p3y = p3.y;
    double dis = 0;
    if (p1x != p2x && p1y==p2y)
    {
        dis = ABS(p3y - p2y);
    }else if (p1x == p2x && p1y!=p2y)
    {
        dis = ABS(p3x - p2x);
    }else if (p1x != p2x && p1y!=p2y){
        double lineK = (p2y - p1y) / (p2x - p1x);
        double lineC = (p2x * p1y - p1x * p2y) / (p2x - p1x);
        dis = ABS(lineK * p3x - p3y + lineC) / (sqrtf(lineK * lineK + 1));
        
    }
    return dis;
}

+(CGPoint)rotate:(CGPoint)p angle:(CGFloat)angle{
    CGPoint pRotated;
    pRotated.x = p.x * cosf(angle) + p.y * sinf(angle);
    pRotated.y = -p.x * sinf(angle) + p.y * cosf(angle);
    return pRotated;
}

+(CGFloat)angle:(CGPoint)cen first:(CGPoint)first second:(CGPoint) second 
{
    CGFloat dx1, dx2, dy1, dy2; 
    CGFloat angle; 
    
    dx1 = first.x - cen.x; 
    dy1 = first.y - cen.y; 
    
    dx2 = second.x - cen.x;     
    dy2 = second.y - cen.y; 
        
    CGFloat A = sqrtf(dx1 * dx1 + dy1 * dy1);
    CGFloat B = sqrtf(dx2 * dx2 + dy2 * dy2);
    
    
    CGFloat AB = (dx1*dy2 - dy1*dx2);
    CGFloat sinM = AB / (A*B);
    sinM = round(sinM*100)/100;
    angle = asinf(sinM);
    
    return -angle; 
} 
+(NSString*)pointToStringMac:(CGPoint)p{
    NSString* str = [NSString stringWithFormat:@"{x=%f;y=%f}", p.x, p.y];
    return str;
}
+(NSString*)pointToStringIPhone:(CGPoint)p{
    NSString* str = [NSString stringWithFormat:@"{%f,%f}", p.x/2, p.y/2];
    return str;
}
+(NSString*)pointToStringIPad:(CGPoint)p{
    p.x += 32;
    NSString* str = [NSString stringWithFormat:@"{%f,%f}", p.x, p.y];
    return str;
}
+(NSImage*)resizeImage:(NSImage*)sourceImage scale:(CGFloat)scale{
    NSImageRep *imgRep = [[sourceImage representations] objectAtIndex:0];
    if (!imgRep) {
        return nil;
    }
    NSSize size = NSMakeSize(imgRep.pixelsWide, imgRep.pixelsHigh);
    
    float resizeWidth = size.width * scale;
    float resizeHeight = size.height * scale;
   
    NSImage *resizedImage = [[[NSImage alloc] initWithSize: NSMakeSize(resizeWidth, resizeHeight)] autorelease];
    
    NSSize originalSize = [sourceImage size];
    
    [resizedImage lockFocus];
    [sourceImage drawInRect: NSMakeRect(0, 0, resizeWidth, resizeHeight) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
    [resizedImage unlockFocus];
    
    return resizedImage;
}

+(NSData*)imageToData:(NSImage*)image ext:(NSString*)ext{
    NSData *dataFromImage = [image TIFFRepresentation];
    NSBitmapImageRep *bmprep = [NSBitmapImageRep imageRepWithData:dataFromImage];
    NSDictionary *imageProperty = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.0], NSImageCompressionFactor, nil];
    NSData *otherTypeImageData = nil;
    if ([[ext lowercaseString] isEqualToString:@"jpg"]||[[ext lowercaseString] isEqualToString:@"jpeg"]) {
        otherTypeImageData = [bmprep representationUsingType:NSJPEGFileType properties:imageProperty];
    }else if ([[ext lowercaseString] isEqualToString:@"png"]) {
        otherTypeImageData = [bmprep representationUsingType:NSPNGFileType properties:imageProperty];
    }else if ([[ext lowercaseString] isEqualToString:@"bmp"]) {
        otherTypeImageData = [bmprep representationUsingType:NSBMPFileType properties:imageProperty];
    }
    
    return otherTypeImageData;
}

@end
